<?php
class LanguagesController extends AppController
{
	var $name = 'Languages';

	var $helpers  = array('Form','Html');
	
	var $components = array("RequestHandler");
	function admin_index($status=null)
	{
		$this->pageTitle = __('Languages');
		$languages = Language::getLanguages();
		$savedLanguages = $this->Language->find("all");
		$this->set('languages',$languages);
		$this->set("savedLanguages",$savedLanguages);
	}

	function admin_changeStatus($status=null)
	{
		if($this->data)
		{
			if(!empty($this->data['Language']))
			{
				foreach($this->data['Language']['id'] as $langauge)
				{
					$data[] = array('id'=>$langauge,'status'=>$this->data['Language']['status']);
				}
				$this->Language->saveAll($data);
				$this->redirect(array("action"=>"index"));
			}
		}
		if($status)
			$conditions ='status='.$status;
		else
			$conditions ='';
		$languages = $this->Language->find('list',array('conditions'=>$conditions,'fields'=>'id,name'));
		$this->set('status',$status);
		$this->set('languages',$languages);
		$this->render('admin_changeStatus');
	}

	function admin_edit($id=null)
	{
		$this->pageTitle = __('Languages');
		if(!$id)
		{
			$this->redirect('/admin/languages');
		}
		if($this->data)
		{
			$this->Language->id = $id;
			
			$this->Language->save($this->data);
			$this->redirect(array("action"=>"index"));
		}
		$this->data = $this->Language->findById($id);
		$this->render('admin_edit');
	}

	function setLanguage($langId=null)
	{
		if(!$langId)
		{
			$langId  = $this->Language->field('lang_key',array('status ='.LANG_DEFAULT));
		}
		$this->Session->write('defaultLanguage',$langId);
		//$time= time()+(24*3600*15);
		//setcookie('defaultLanguage',$langId,$time,'/');
		if(strpos($this->referer(),'setLanguage')==false)
			$this->redirect($this->referer());
		else
			$this->redirect('/');
	}

	function admin_setAsDefault($code = null)
	{
		if(empty($code))
		{
			$this->redirect($this->referer());
		}
		$saved =  false;
		$id = $this->Language->getLanguageId($code);
		$this->Language->id = $id;
		$this->Language->saveField('is_default',1);
		$saved = $this->Language->saveField('status',Language::ACTIVE);
		if($saved != false)
		{
			$this->Language->updateAll(array('is_default'=>0),array('not'=>array('id'=>$id)));
		}
		$this->Language->delPersistentCacheFile($code);
		$this->Language->setLanguageConfiguration(false);
		$this->successMsg(__('Default language has been changed!'));
		$this->redirect(array("action"=>"index"));
	}
	
	function admin_deactivate($code)
	{
		$id = $this->Language->getLanguageId($code);
		if($this->Language->hasAny(array("id"=>$id,'is_default'=>$id)))
		{
			$this->errorMsg(__("Changing status of default language to inactive is not allowed"));
			$this->redirect($this->referer());
		}
		$this->Language->id = $id;
		$this->Language->saveField("status",Language::INACTIVE);
		$this->successMsg(__("Language has been deactivated"));
		$this->redirect($this->referer());
		
	}
	function admin_activate($code)
	{
		$id = $this->Language->getLanguageId($code);
		$this->Language->id = $id;
		$this->Language->saveField("status",Language::ACTIVE);
		$this->successMsg(__("Language has been activated"));
		$this->redirect($this->referer());
		
	}
	function admin_uploadLanguageFile($code = null)
	{	
		$this->pageTitle = __('Upload language file');
		if(!$code)
		{
			$this->redirect($this->referer('/'));
		}
		if($this->data)
		{
			$uploadedFileName = $this->data['Language']['languageFile']['name'];
			$uploadedFile = $this->data['Language']['languageFile']['tmp_name'];
			if(strrchr($uploadedFileName,'.')!='.po')
			{
				$this->errorMsg(__('Please upload a valid PO file with po extension!'));
			}
			else
			{
				if($this->Language->addLocaleFile($code,file_get_contents($this->data['Language']['languageFile']['tmp_name'])))
				{
					$this->Language->delPersistentCacheFile($code);
					$this->successMsg(__("Language file has been updated"));
					$this->redirect(array('action'=>'index'));
				}
			}
			
		}
	}
	
	function admin_translate($code = null)
	{
		$language = $this->Language->getLanguage($code);
		if(!$language)
		{
			$this->errorMsg(__("Invalid language"));
			$this->redirect(array("action"=>"index"));
			return;
		}
		$this->pageTitle = sprintf(__("Translate %s"),$language['language']);
		if($this->data)
		{
			if(isset($this->data['Language']['json']))
			{
				$json = $this->data['Language']['json'];
				unset($this->data['Language']['json']);
				$json = json_decode($json,true);
				$this->data['Language']['translation'] = $json;
			}
			if($this->Language->makePoFile($code,$this->data['Language']['translation']))
			{
				$this->successMsg(sprintf(__("Translations for %s has been saved."),$language['language']));
				$this->redirect(array("action"=>"index"));
			}
		}
		$tranlateStrings = $this->Language->getTranslationsStrings($code);
		$this->set("tranlateStrings",$tranlateStrings);
	}
	
	function setSelectedLanguage($code)
	{
		if($this->Language->isActiveLanguage($code))
		{
			$this->Session->write("Config.language", $code);
			Configure::write("Language.userLanguage",$code);
		}
		$url = "/";
		if(isset($_GET['referer']))
		{
			$url = $_GET['referer'];
		}
		if($url == "//")
		{
			$url = "/";
		}
		$this->redirect($url);
	}

}

?>